package com.tydic.system.init;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tydic.system.domain.SysOssConfig;
import com.tydic.system.mapper.SysOssConfigMapper;
import com.tydic.system.properties.MinioProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
@Slf4j
public class MinioInit implements InitializingBean {

    private final SysOssConfigMapper sysOssConfigMapper;

    private final MinioProperties minioProperties;

    @Override
    public void afterPropertiesSet() throws Exception {
        SysOssConfig minio = sysOssConfigMapper.selectOne(Wrappers.lambdaQuery(SysOssConfig.class).eq(SysOssConfig::getConfigKey, "minio"), false);
        minio.setAccessKey(minioProperties.getAccessKey());
        minio.setSecretKey(minioProperties.getSecretKey());
        minio.setEndpoint(minioProperties.getEndpoint());
        minio.setBucketName(minioProperties.getBucketName());
        minio.setDomain(minioProperties.getDomain());
        minio.setIsHttps(minioProperties.isHttps() ? "Y" : "N");
        sysOssConfigMapper.updateById(minio);
    }
}
